package com.icart.controller.personController;

import com.icart.context.HttpUserContext;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;

import com.icart.repositories.WoservbookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/person")
public class MyservbookController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    WoservbookRepository woservbookRepository;

    /**/
    @RequestMapping("/myservbook")
    public String myservbook(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

       List list=woservbookRepository.createNativeQuery("select (select n.chnname from bi_chn n where n.chnid=k.chnid)chnname," +
               "(select m.dictname from sa_db_dictitem m where m.groupid='WO_SERVBOOKSTAT' and m.dictid=k.stat)statname,k.bookseq,k.bookdate,k.booktime,k.remark,k.stat," +
               "(select (select n.servitemname from bi_servitem n where n.servitemid=m.servitemid)servitemname from wo_servbookitem m where m.bookseq=k.bookseq limit 1)servitemname" +
               " from wo_servbook k where k.custid="+userContext.getCustid());
        model.addAttribute("woServbook",list);
        return "person/myservbook/myservbook";
    }

    /*预约详情*/
    @RequestMapping("/myservbookitem")
    public String myservbookitem(@RequestParam(name = "bookseq",required = false) String bookseq, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        List list=woservbookRepository.createNativeQuery("select (select n.servitemname from bi_servitem n where n.servitemid=m.servitemid)servitemname,m.cnt " +
                "from wo_servbookitem m where m.bookseq="+bookseq);
        model.addAttribute("list",list);

        List list1=woservbookRepository.createNativeQuery("select k.bookseq,k.carno,(select n.chnname from bi_chn n where n.chnid=k.chnid)chnname," +
                "k.bookdate,k.booktime,k.createtime " +
                "from wo_servbook k where k.bookseq="+bookseq+" and k.custid="+userContext.getCustid());
        model.addAttribute("servbook",list1.get(0));
        return "person/myservbook/myservbookitem";
    }

    /*取消预约*/
    @RequestMapping("/cancelbook")
    public String cancelbook(@RequestParam(name = "bookseq",required = false) String bookseq, Model model) {
        woservbookRepository.cancelbook(bookseq);
        return "redirect:/person/myservbook";
    }

}